package com.ogilvy.cms.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.ogilvy.cms.bean.CmsLog;
import com.ogilvy.cms.bean.CmsPage;
import com.ogilvy.cms.bean.CmsTemplate;
import com.ogilvy.cms.bean.CmsUser;
import com.ogilvy.cms.util.ServiceDispatcher;
import com.ogilvy.common.JSONBean;
import com.ogilvy.common.MetaData;
import com.ogilvy.common.UtilTools;
import com.ogilvy.frame.action.BaseAction;
import com.ogilvy.frame.util.CmsConstants;

@SuppressWarnings("serial")
@Scope("prototype")
@Controller("cms.action.CmsPageAction")
public class CmsPageAction extends  BaseAction {
	@Autowired
	protected  ServiceDispatcher  serviceDispatcher;
	private String rows;//前台以JSON的形式传递数据
	private int start;//分页开始
	private int limit;//每页显示量

	private CmsPage  cmsPage = new CmsPage();
	List<CmsPage> datalist ;
	
	  public String page()
	  {
	    return "page";
	  }
	
	

	/**查询page**/
	public void query(){
		datalist = serviceDispatcher.getCmsPageService().query(cmsPage);
		//采用集合分页
		List pageList = UtilTools.queryForPage(start, limit, datalist);
		JSONBean jbean = new JSONBean();
		MetaData md = new MetaData();
		md.setIdProperty("cmpId");//关联将要显示列表对应的bean实体的主键ID
		md.reflectFields("com.ogilvy.cms.bean.CmsPage");//关联将要显示列表对应的bean实体
		jbean.setMetaData(md);
		jbean.setRows(pageList);////
		jbean.setSuccess(true);
		jbean.setTotal(datalist.size());
		
		ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
		
		//String jsonString = UtilTools.encodeObject2Json(jbean);
		String jsonString = UtilTools.encodeObject2Json(jbean,"MM/dd/yyyy HH:mm:ss");
		
		PrintWriter out;
		try {
			out = ServletActionContext.getResponse().getWriter();
			out.print(jsonString);
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/** 新增 */
	public void add(){
		System.out.println("cmspageAction :: add method ");
		CmsUser cmsuser = (CmsUser)ServletActionContext.getRequest().getSession().getAttribute(CmsConstants.ADMIN_USER);
		String cmpAdduser = cmsuser.getCmuName();//获取session中对象的姓名
		//日志记录
		CmsLog  cmsLog = UtilTools.buildLog("栏目管理", CmsLog.Add, "增加栏目", cmpAdduser);
		serviceDispatcher.getCmsLogService().add(cmsLog);
		
		net.sf.json.JSONObject js = net.sf.json.JSONObject.fromObject(rows);
		Integer	cmpCmtId = js.getInt("cmtName");				//模板 名称对应的id
		System.out.println("------->"+cmpCmtId);
		String cmtName=js.getString("cmtName");
		System.out.println(cmtName);
		System.out.println("-------------------->");
		Integer parentCmpId = js.getInt("parentCmpId");			//父页 
		String cmpLayerid= js.getString("cmpLayerid");			//级联ID 
		String cmpTitleName= js.getString("cmpTitleName");		//页面名称 
		String cmpName= js.getString("cmpName");				//浏览器名称 
		String cmpPubStatus= js.getString("cmpPubStatus");		//状态 
		String cmpDirName=  js.getString("cmpDirName");			//文件夹名称 
		Integer cmpOrderNum=  js.getInt("cmpOrderNum");			//排序值 
		String cmpNavigationName = js.getString("cmpNavigationName");//导航名称 
		Integer cmpIsWebsite = js.getInt("cmpIsWebsite");		//是否站点 
		Integer	cmpIsColumn = js.getInt("cmpIsColumn");			//是否栏目 
		Integer cmpIsNavigation = js.getInt("cmpIsNavigation");	//是否导航 1 是 
		Integer	cmpIsWebmap = js.getInt("cmpIsWebmap");			//是否网站地图 1 是 
		Integer cmpIsIndexTop = js.getInt("cmpIsIndexTop"); 	//是否首页置顶 
		Integer cmpIsCloumnTop = js.getInt("cmpIsCloumnTop"); 	//是否栏目置顶 
		Integer cmpTitleImg = js.getInt("cmpTitleImg");			//题头图 
		String cmpDesc = js.getString("cmpDesc");				//网页简介 
		String cmpViewtime = js.getString("cmpViewtime");		//显示时间 
		String cmpSeoKeywords = js.getString("cmpSeoKeywords");	//seo关键字
		String cmpSeoDescription = js.getString("cmpSeoDescription");//seo描述
		Integer cmpCssId = js.getInt("cmpCssId");				//样式表 
		Integer	cmpAddCssId = js.getInt("cmpAddCssId");			//新增样式 
		String cmpStartTime = js.getString("cmpStartTime");		//开始时间 
		String cmpEndTime = js.getString("cmpEndTime");			//结束时间 
		String cmpOuterTrackingcode = js.getString("cmpOuterTrackingcode");//监听代码  
		String cmpContent = js.getString("cmpContent");			//页面数据 
		Integer cmpVisits = js.getInt("cmpVisits");				//访问数 此处作了修改
		//String cmpAddtime = UtilTools.getCurrrentDate();		//添加时间,获取添加时的系统时间
		//String cmpLastuser = js.getString("cmpLastuser");		//最后修改人
		//String cmpLasttime = js.getString("cmpLasttime");		//最后修改时间



		//主键自增长
		//cmsPage.setCmpCmtId(Integer.valueOf(cmpCmtId));;
		CmsTemplate cmt = new CmsTemplate();
		cmt.setCmtId(Integer.valueOf(cmpCmtId));
		cmsPage.setCmt(cmt);
		cmsPage.setParentCmpId(Integer.valueOf(parentCmpId));
		cmsPage.setCmpLayerid(cmpLayerid);
		cmsPage.setCmpTitleName(cmpTitleName);
		cmsPage.setCmpName(cmpName);
		cmsPage.setCmpPubStatus(cmpPubStatus);
		cmsPage.setCmpDirName(cmpDirName);
		cmsPage.setCmpOrderNum(Integer.valueOf(cmpOrderNum));
		cmsPage.setCmpNavigationName(cmpNavigationName);
		cmsPage.setCmpIsWebsite(Integer.valueOf(cmpIsWebsite));
		cmsPage.setCmpIsColumn(Integer.valueOf(cmpIsColumn));
		cmsPage.setCmpIsNavigation(Integer.valueOf(cmpIsNavigation));
		cmsPage.setCmpIsWebmap(Integer.valueOf(cmpIsWebmap));
		cmsPage.setCmpIsIndexTop(Integer.valueOf(cmpIsIndexTop));
		cmsPage.setCmpIsColumn(Integer.valueOf(cmpIsCloumnTop));		//此处作了修改
		cmsPage.setCmpTitleImg(Integer.valueOf(cmpTitleImg));
		cmsPage.setCmpDesc(cmpDesc);
//		cmpViewtime = cmpViewtime.substring(0,10);
//		System.out.println(cmpViewtime);
		cmsPage.setCmpViewtime(cmpViewtime);
		cmsPage.setCmpSeoKeywords(cmpSeoKeywords);
		cmsPage.setCmpSeoDescription(cmpSeoDescription);
		cmsPage.setCmpCssId(Integer.valueOf(cmpCssId));
		cmsPage.setCmpAddCssId(Integer.valueOf(cmpAddCssId));
		cmsPage.setCmpStartTime(cmpStartTime);	//开始时间
		cmsPage.setCmpEndTime(cmpEndTime);		//结束时间
		cmsPage.setCmpOuterTrackingcode(cmpOuterTrackingcode);
		cmsPage.setCmpContent(cmpContent);
		cmsPage.setCmpVisits(Integer.valueOf(cmpVisits));
		cmsPage.setCmpAdduser(cmpAdduser);		//session获取当前用户
		cmsPage.setCmpAddtime(new Date());		//获取当前系统时间
		//cmsPage.setCmpLastuser(cmpLastuser);	//新增时，没有修改人
		//cmsPage.setCmpLasttime(cmpLasttime);	//此时还没有修改时间
		CmsPage cmpage = serviceDispatcher.getCmsPageService().add(cmsPage);
		
		JSONBean bean = new JSONBean();
		bean.setSuccess(true);
		// 需要新建一个具有id的vo返回给前台以便通知前台保存成功，不然前台一直处于编辑状态
		List rows = new ArrayList();
		rows.add(cmpage);
		bean.setRows(rows);
		
		//String jsonString = UtilTools.encodeObject2Json(bean);
		String jsonString = UtilTools.encodeObject2Json(bean,"MM/dd/yyyy HH:mm:ss");
		
		PrintWriter out;
		try {
			out = ServletActionContext.getResponse().getWriter();
			out.print(jsonString);
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	/** 修改 */
	public void modify(){
		System.out.println("cmsPage modify");
		CmsUser cmsuser = (CmsUser)ServletActionContext.getRequest().getSession().getAttribute(CmsConstants.ADMIN_USER);
		String cmpLastuser= cmsuser.getCmuName();
		// 栏目 修改日志添加
		CmsLog  cmslog = UtilTools.buildLog("栏目管理", CmsLog.MODIFY, "修改栏目信息", cmpLastuser);
		serviceDispatcher.getCmsLogService().add(cmslog);
		ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
		net.sf.json.JSONObject js = net.sf.json.JSONObject.fromObject(rows);
		Integer cmpId = js.getInt("cmpId");
		Integer	cmpCmtId = js.getInt("cmpCmtId");//获取模板Id
		cmpCmtId= js.getInt("cmtid");
		System.out.println(cmpCmtId+"************************");
		Integer parentCmpId = js.getInt("parentCmpId");
		String cmpLayerid = js.getString("cmpLayerid");
		String cmpTitleName = js.getString("cmpTitleName");
		String cmpName = js.getString("cmpName");
		String cmpPubStatus = js.getString("cmpPubStatus");
		String cmpDirName =  js.getString("cmpDirName");
		Integer cmpOrderNum = js.getInt("cmpOrderNum");
		String cmpNavigationName = js.getString("cmpNavigationName");
		Integer cmpIsWebsite = js.getInt("cmpIsWebsite");
		Integer	cmpIsColumn = js.getInt("cmpIsColumn");
		Integer cmpIsNavigation = js.getInt("cmpIsNavigation");
		Integer	cmpIsWebmap = js.getInt("cmpIsWebmap");
		Integer	cmpIsIndexTop = js.getInt("cmpIsIndexTop");
		Integer cmpIsCloumnTop = js.getInt("cmpIsCloumnTop");
		Integer cmpTitleImg = js.getInt("cmpTitleImg");
		String cmpDesc = js.getString("cmpDesc");
		String cmpViewtime = js.getString("cmpViewtime");
		String cmpSeoKeywords = js.getString("cmpSeoKeywords");
		String cmpSeoDescription = js.getString("cmpSeoDescription");
		Integer cmpCssId = js.getInt("cmpCssId");
		Integer	cmpAddCssId = js.getInt("cmpAddCssId");
		String cmpStartTime = js.getString("cmpStartTime");
		String cmpEndTime = js.getString("cmpEndTime");
		String cmpOuterTrackingcode = js.getString("cmpOuterTrackingcode");
		String cmpContent = js.getString("cmpContent");
		Integer cmpVisits = js.getInt("cmpVisits");		//此处作了修改  应该是Integer类型
//		String cmpAdduser = js.getString("cmpAdduser");	//此处作了修改  应该是String类型  修改时，添加人不变
//		String Addtime = js.getString("cmpAddtime");	//修改时，获取添加时间
		//String cmpLastuser = js.getString("cmpLastuser");
	
		//String cmpLasttime = UtilTools.getCurrrentDate();//此时，修改时间为系统时间
		
		cmsPage = serviceDispatcher.getCmsPageService().get(cmpId);
//		System.out.println("------------------------->");
//		System.out.println(cmpId);
//		System.out.println(parentCmpId);
//		System.out.println("<------------------------------");
		cmsPage.setCmpId(cmpId);
		cmsPage.setParentCmpId(parentCmpId);
		cmsPage.setCmpLayerid(cmpLayerid);
		cmsPage.setCmpTitleName(cmpTitleName);
		cmsPage.setCmpName(cmpName);
		cmsPage.setCmpPubStatus(cmpPubStatus);
		//cmsPage.setCmpCmtId(cmpCmtId);
		cmsPage.setCmpDirName(cmpDirName);
		cmsPage.setCmpOrderNum(cmpOrderNum);
		cmsPage.setCmpNavigationName(cmpNavigationName);
		cmsPage.setCmpIsWebsite(cmpIsWebsite);
		cmsPage.setCmpIsColumn(cmpIsColumn);
		cmsPage.setCmpIsNavigation(cmpIsNavigation);
		cmsPage.setCmpIsWebmap(cmpIsWebmap);
		cmsPage.setCmpIsIndexTop(cmpIsIndexTop);
		cmsPage.setCmpIsColumn(cmpIsCloumnTop);
		cmsPage.setCmpTitleImg(cmpTitleImg);
		cmsPage.setCmpDesc(cmpDesc);
		cmsPage.setCmpViewtime(cmpViewtime);
		cmsPage.setCmpSeoKeywords(cmpSeoKeywords);
		cmsPage.setCmpSeoDescription(cmpSeoDescription);
		cmsPage.setCmpCssId(cmpCssId);
		cmsPage.setCmpAddCssId(cmpAddCssId);
		cmsPage.setCmpStartTime(cmpStartTime);	//开始时间
		cmsPage.setCmpEndTime(cmpEndTime);		//结束时间
		cmsPage.setCmpOuterTrackingcode(cmpOuterTrackingcode);
		cmsPage.setCmpContent(cmpContent);
		cmsPage.setCmpVisits(cmpVisits);		//访问数
//		cmsPage.setCmpAdduser(cmpAdduser);
//		cmsPage.setCmpAddtime(cmpAddtime);		//获取添加时间
		cmsPage.setCmpLastuser(cmpLastuser);
		cmsPage.setCmpLasttime(new Date());	//获取系统时间
		serviceDispatcher.getCmsPageService().modify(cmsPage);
		
		JSONBean bean = new JSONBean();
		bean.setSuccess(true);
		
		//String jsonString = UtilTools.encodeObject2Json(bean);
		String jsonString = UtilTools.encodeObject2Json(bean,"MM/dd/yyyy HH:mm:ss");
		
		PrintWriter out;
		try {
			out = ServletActionContext.getResponse().getWriter();
			out.print(jsonString);
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/** 删除  **/
	/**  删除多个 采用basedao中的 **/
	public void del(){
		CmsUser  cmsSessionUser = (CmsUser)ServletActionContext.getRequest().getSession().getAttribute(CmsConstants.ADMIN_USER);
		String   delPageUser = cmsSessionUser.getCmuName();
		//栏目删除日志添加
		CmsLog cmslog = UtilTools.buildLog("栏目管理", CmsLog.DELETE, "删除栏目", delPageUser);
		serviceDispatcher.getCmsLogService().add(cmslog);
		
		ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
		net.sf.json.JSONArray objArr = net.sf.json.JSONArray.fromObject(rows);
        for(int i=0;i<objArr.size();i++)
        {
        	net.sf.json.JSONObject js=objArr.getJSONObject(i);
        	//将 String 转化为 Integer 删除
        	String  cmpIdStr = js.getString("cmpId");
        	System.out.println("---------->获取要删除的id"+cmpIdStr);
        	Integer cmpId = Integer.valueOf(cmpIdStr);
        	serviceDispatcher.getCmsPageService().del(cmpId);
		}
		
		
        //获取数据
        StringBuffer jsonString =new StringBuffer();
        jsonString.append("{");
        jsonString.append("success:true,");
        jsonString.append("msg:'删除成功'");
        jsonString.append("}");
		PrintWriter out;
		try {
			out = ServletActionContext.getResponse().getWriter();
			out.print(jsonString);
			out.flush();
			out.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	private Integer cmpId;
	public Integer getCmpId() {
		return cmpId;
	}

	public void setCmpId(Integer cmpId) {
		this.cmpId = cmpId;
	}

	public CmsPage getCmsPage() {
		return cmsPage;
	}
	public void setCmsPage(CmsPage cmsPage) {
		this.cmsPage = cmsPage;
	}

	public List<CmsPage> getdatalist() {
		return datalist;
	}
//	public void setPagelist(List<CmsPage> pagelist) {
//		this.datalist = pagelist;
//	}
	
	

	public String getRows() {
		return rows;
	}
	public void setRows(String rows) {
		this.rows = rows;
	}
	public int getStart() {
		return start;
	}
	public void setStart(int start) {
		this.start = start;
	}
	public int getLimit() {
		return limit;
	}
	public void setLimit(int limit) {
		this.limit = limit;
	}
	public List<CmsPage> getCmspagelist() {
		return datalist;
	}
	public void setCmspagelist(List<CmsPage> cmspagelist) {
		this.datalist = cmspagelist;
	}
	
	
	
}
